//
//  AddBankViewController.m
//  Findif
//
//  Created by Mac on 2018/11/8.
//  Copyright © 2018年 Cicel Chen. All rights reserved.
//

#import "AddBankViewController.h"

@interface AddBankViewController ()<UITextFieldDelegate>

@property (nonatomic, weak)UILabel * titleLabel;
@property (nonatomic, weak)UIView * backView;
@property (nonatomic, strong)UILabel * nameLabel;
@property (nonatomic, strong)UILabel * carNumLabel;
@property (nonatomic, strong)UILabel * telLabel;
@property (nonatomic, weak)UIView * lineView;
@property (nonatomic, weak)UIView * twoLineView;
@property (nonatomic, strong)UITextField * nameTextField;
@property (nonatomic, strong)UITextField * carNumTextField;
@property (nonatomic, strong)UITextField * telTextField;
@property (nonatomic, weak)UIButton * nextButton;

@end

@implementation AddBankViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self setupNav];
    [self setupUI];
}

- (void)setupNav{
    
    self.title = @"添加银行卡";
    
//    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"back(1)"] style:UIBarButtonItemStylePlain target:nil action:nil];
//    
//    self.navigationItem.backBarButtonItem = backItem;
}


- (void)setupUI{
    
    self.view.backgroundColor = ColorWithRGB(0xF7F8F9);
    
    UILabel * titleLabel = [UILabel new];
    
    titleLabel.text = @"请绑定持卡人本人的银行卡";
    titleLabel.textColor = [UIColor lightGrayColor];
    titleLabel.font = [UIFont systemFontOfSize:15 * SCREENH6];
    
    self.titleLabel = titleLabel;
    
    [self.view addSubview:self.titleLabel];
    
    
    UIView * backView = [UIView new];
    
    backView.backgroundColor = ZRWhiteColor;
    backView.layer.cornerRadius = 5;
    backView.layer.masksToBounds = YES;
    backView.userInteractionEnabled = YES;
    
    self.backView = backView;
    
    [self.view addSubview:self.backView];
    
    self.nameLabel = [self setupLabelWithTitle:@"持卡人"];
    [self.backView addSubview:self.nameLabel];
    self.carNumLabel = [self setupLabelWithTitle:@"卡号"];
    [self.backView addSubview:self.carNumLabel];
    self.telLabel = [self setupLabelWithTitle:@"手机号"];
    [self.backView addSubview:self.telLabel];
    
    self.nameTextField = [self setupTextFieldWithTitle:@"请输入持卡人姓名"];
    [self.backView addSubview:self.nameTextField];
    self.carNumTextField = [self setupTextFieldWithTitle:@"请输入银行卡号"];
    [self.backView addSubview:self.carNumTextField];
    self.telTextField = [self setupTextFieldWithTitle:@"请输入开户手机号"];
    [self.backView addSubview:self.telTextField];
    
    UIView * lineView = [UIView new];
    
    lineView.backgroundColor = ColorWithRGB(0xF4F5F6);
    
    self.lineView = lineView;
    
    [self.backView addSubview:self.lineView];
    
    UIView * twoLineView = [UIView new];
    
    twoLineView.backgroundColor = ColorWithRGB(0xF4F5F6);
    
    self.twoLineView = twoLineView;
    
    [self.backView addSubview:self.twoLineView];
    
    UIButton * nextButton = [UIButton new];
    
//    [nextButton setBackgroundColor:ColorWithRGB(0xFFE055)];
    [nextButton setBackgroundColor:ColorWithRGB(0xFCF4B6)];
    [nextButton setTitle:@"提交" forState:UIControlStateNormal];
    [nextButton setTitleColor:[UIColor lightGrayColor] forState:UIControlStateDisabled];
    [nextButton setTitleColor:ZRBlackColor forState:UIControlStateNormal];
    [nextButton addTarget:self action:@selector(clickNextButton) forControlEvents:UIControlEventTouchUpInside];
    nextButton.titleLabel.font = [UIFont systemFontOfSize:15 * SCREENH6];
    nextButton.enabled = NO;
    
    self.nextButton = nextButton;
    
    [self.view addSubview:self.nextButton];
}


- (void)viewDidLayoutSubviews{
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.view).offset(15 * SCREENH6);
        make.top.mas_equalTo(self.view).offset(kNavHeight + kStatusBarHeight +10 * SCREENH6);
    }];
    
    [self.backView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.view).offset(15 * SCREENH6);
        make.trailing.mas_equalTo(self.view).offset(-15 * SCREENH6);
        make.top.mas_equalTo(self.titleLabel.mas_bottom).offset(10 * SCREENH6);
        make.height.mas_equalTo(125 * SCREENH6);
    }];
    
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.backView).offset(10 * SCREENH6);
        make.top.mas_equalTo(self.backView).offset(15 * SCREENH6);
    }];
    
    [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.backView).offset(40 * SCREENH6);
        make.leading.mas_equalTo(self.nameLabel);
        make.trailing.mas_equalTo(self.backView).offset(-10 * SCREENH6);
        make.height.mas_equalTo(1 * SCREENH6);
    }];
    
    [self.nameTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.nameLabel);
        make.height.mas_equalTo(35 * SCREENH6);
        make.leading.mas_equalTo(self.backView).offset(85 * SCREENH6);
        make.trailing.mas_equalTo(self.lineView);
    }];
    
    [self.carNumLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.lineView);
        make.top.mas_equalTo(self.lineView.mas_bottom).offset(13 * SCREENH6);
    }];
    
    [self.carNumTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.height.mas_equalTo(self.nameTextField);
        make.centerY.mas_equalTo(self.carNumLabel);
    }];
    
    [self.twoLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.height.mas_equalTo(self.lineView);
        make.top.mas_equalTo(self.lineView.mas_bottom).offset(40 * SCREENH6);
    }];
    
    [self.telLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.twoLineView);
        make.top.mas_equalTo(self.twoLineView.mas_bottom).offset(15 * SCREENH6);
    }];
    
    [self.telTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.height.mas_equalTo(self.carNumTextField);
        make.centerY.mas_equalTo(self.telLabel);
    }];
    
    [self.nextButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view);
        make.top.mas_equalTo(self.backView.mas_bottom).offset(20 * SCREENH6);
        make.height.mas_equalTo(30 * SCREENH6);
        make.width.mas_equalTo(250 * SCREENH6);
    }];
}

- (UILabel *)setupLabelWithTitle:(NSString *)title{
    
    UILabel * label = [UILabel new];
    
    label.textColor = ZRBlackColor;
    label.text = title;
    label.font = [UIFont systemFontOfSize:15 * SCREENH6];
    
    return label;
}


- (UITextField *)setupTextFieldWithTitle:(NSString *)title{
    
    UITextField * textFiled = [[UITextField alloc] init];
    
    textFiled.borderStyle = UITextBorderStyleNone;
    textFiled.backgroundColor = ZRWhiteColor;
    //设置光标颜色
    textFiled.tintColor = ZRBlackColor;
    //修改占位符文字颜色
    NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
    attrs[NSForegroundColorAttributeName] = ColorWithRGB(0xa5a5a5);
    attrs[NSFontAttributeName] = [UIFont fontWithName:@"PingFangSC-Regular" size:15 * SCREENH6];
    textFiled.attributedPlaceholder = [[NSAttributedString alloc] initWithString:title attributes:attrs];
    textFiled.textColor = ZRBlackColor;
    //    textFiled.leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 22 * SCREENH6, 0)];
    //    //设置显示模式为永远显示(默认不显示)
    //    textFiled.leftViewMode = UITextFieldViewModeAlways;
    textFiled.font = [UIFont fontWithName:@"PingFangSC-Regular" size:15 * SCREENH6];
//    textFiled.keyboardType = UIKeyboardTypePhonePad;
    textFiled.delegate = self;
    
    return textFiled;
}

- (void)clickNextButton{
    
    [[ZRNetworkTool sharedTool] addBingBankCardResultWithBank_card_account_holder:self.nameTextField.text WithBank_card_no:self.carNumTextField.text WithBank_card_phone:self.telTextField.text WithBlock:^(BOOL success, id responseData) {
        
        if (success) {
            
            UIAlertController* alert = [UIAlertController alertControllerWithTitle:@""
                                                                           message:@"恭喜你绑定成功！"
                                                                    preferredStyle:UIAlertControllerStyleAlert];
            
            UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel
                                                                  handler:^(UIAlertAction * action) {
                                                                      
                                                                      [self.navigationController popViewControllerAnimated:YES];
                                                                  }];
            
            [alert addAction:defaultAction];
            [self presentViewController:alert animated:YES completion:nil];
        }else{
            
            UIAlertController* alert = [UIAlertController alertControllerWithTitle:@""
                                                                           message:responseData[@"message"]
                                                                    preferredStyle:UIAlertControllerStyleAlert];
            
            UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel
                                                                  handler:^(UIAlertAction * action) {
                                                                      
                                                                      
                                                                  }];
            
            [alert addAction:defaultAction];
            [self presentViewController:alert animated:YES completion:nil];
        }
    }];
}

- (void)textFieldDidEndEditing:(UITextField *)textField{
    
    [self changeButtonState];
}


- (void)changeButtonState{
    
    if (self.nameTextField.text.length > 0 && self.carNumTextField.text.length > 0 && self.telTextField.text.length > 0) {
        
        self.nextButton.enabled = YES;
        [self.nextButton setBackgroundColor:ColorWithRGB(0xFFE055)];
    }
}


@end
